package cn.tedu.property_management_system.controller;

import cn.tedu.property_management_system.entity.*;
import cn.tedu.property_management_system.entity.DTO.ComplaintAddNewDTO;
import cn.tedu.property_management_system.entity.VO.ComplaintVO;
import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.utils.JsonResult;
import cn.tedu.property_management_system.utils.linstener.ComplaintUploadListener;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import cn.tedu.property_management_system.service.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import static cn.tedu.property_management_system.utils.Constant.HANDLE_SUCCESS;
import static cn.tedu.property_management_system.utils.JsonResult.State.ERR_FILE_UPLOAD_EXCEPTION;

@Slf4j
@RestController
@RequestMapping("complaint")
@PreAuthorize("hasAnyAuthority('/admin/core/complaint')")
public class CompliantController {

    @Autowired
    ComplaintUploadListener complaintUploadListener;

    @Autowired
    IComplaintService complaintService;

    @PostMapping("addCompliant")
    @PreAuthorize("hasAnyAuthority('/admin/core/complaint','/user/base/complaint')")
    public JsonResult addCompliant(@RequestBody ComplaintAddNewDTO complaintAddNewDTO){
        complaintService.insert(complaintAddNewDTO);
        return JsonResult.OK();
    }

    @GetMapping("loadCompliant/{pageNum}/{PageSize}")
    public JsonResult<PageInfo<ComplaintVO>> loadCompliant(
            @PathVariable("pageNum")Integer pageNum,@PathVariable("PageSize")Integer PageSize){
        PageInfo<ComplaintVO> complaintVOPageInfo = complaintService.loadCompliant(pageNum, PageSize);
        return JsonResult.OK(complaintVOPageInfo);
    }


    @GetMapping("loadCompliant/personal")
    @PreAuthorize("hasAnyAuthority('/admin/core/complaint','/user/base/complaint')")
    public List<ComplaintVO> loadPersonalCompliant(){
        log.debug("加载个人投诉列表");
        List<ComplaintVO> complaintVOS = complaintService.loadPersonalCompliant();
        return complaintVOS;
    }

    @GetMapping("deleteCompliant/{id}")
    public JsonResult deleteCompliant(@PathVariable int id,HttpSession session){
        complaintService.deleteCompliant(id);
        return JsonResult.OK();
    }

    @GetMapping("handleCompliant/{id}")
    public Integer handleCompliant(@PathVariable int id, HttpSession session){
        User user =(User) session.getAttribute("user");
        complaintService.handleCompliant(id,user.getId());
        return HANDLE_SUCCESS;
    }

    @GetMapping("loadCompliantType")
    @PreAuthorize("hasAnyAuthority('/admin/core/complaint','/user/base/complaint')")
    public List<ComplaintType> loadCompliantType(){
        log.debug("查询投诉类型");
        List<ComplaintType> complaintTypes = complaintService.loadCompliantType();
        return complaintTypes;
    }

    @GetMapping("screenCompliant/{comId}/{status}")
    public List<ComplaintVO> screenCompliant(@PathVariable Integer comId,@PathVariable Integer status){
        List<ComplaintVO> complaintVOS = complaintService.screenCompliant(comId, status);
        return complaintVOS;
    }

    @PostMapping("bathDelete")
    public JsonResult bathDelete(@RequestBody Integer[] deleteComplaintArr){
        complaintService.bathDelete(deleteComplaintArr);
        return JsonResult.OK();
    }

    @RequestMapping("bathUpDate")
    public JsonResult bathUpDate(MultipartFile file){
        try {
            log.debug("准备开始接收上传文件");
            //创建一个读文件的对象
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(),
                    Complaint.class, complaintUploadListener);
            //用读文件对象创建工作表
            ExcelReaderSheetBuilder sheet = read.sheet();
            //读取表
            sheet.doRead();
        } catch (IOException e) {
            throw new ServiceException(ERR_FILE_UPLOAD_EXCEPTION,"上传出现异常");
        }
        return JsonResult.OK();
    }

    @RequestMapping("bathOutput")
    public void bathOutput(HttpServletResponse response){
        complaintService.bathOutput(response);
    }
}
